<template>
  <el-form class="option" ref="formRef" :label-width="labelWidth" size="small" :rules="prules" :model="calcPricingParam" >
    <el-form-item label="计算组件">
      <!--  计算组件的过滤通过queryParam来做    -->
      <baseCombox :url="url.data" ref="baseComboxRef"
                  :queryParam="queryParam"
                  valueField="ppCode"
                  labelField="ppName"
                  size="small"
                  :style="{width: '100%'}" placeholder="请选中计算组件" v-model="calcPricingParam.calcPPCode"/>
    </el-form-item>
    <slot name="price"></slot>
    <el-form-item label="价格类型">
      <dictCombox v-model="calcPricingParam.spotQuoteType" :filterKey="data.filterList"
                   :clearable='false' @change="setSpotPriceProxy()" dictType="spotQuoteType"/>
    </el-form-item>
    <el-form-item label="标的价格" prop="spotPrice">
      <div v-if="calcPricingParam.spotQuoteType==0" class="wrapper">
        <InputAmount v-model:value="calcPricingParam.spotPrice"  precision="2" append="元"/>
        <el-button @click="setSpotPriceProxy" :style="{float:right}"  icon="RefreshLeft" circle />
      </div>
      <InputAmount v-else v-model:value="calcPricingParam.spotPrice"  precision="2" append="元"/>
    </el-form-item>
    <curvePriceCombox ref="discountRef" :label-width="labelWidth" labelName="无风险利率"  :index="props.index"
                        :min="-100"
                      type="ircurve" :calcPricingParam="props.calcPricingParam.discount" />
    <curvePriceCombox ref="dividendRef" :calcPricingParam="props.calcPricingParam.dividend" :index="props.index"
                      type="divyield"  :min="-100"
                      :label-width="labelWidth" labelName="红利率" url="/pricing/comboBox/queryDivyield.action"/>
    <curvePriceCombox ref="volSurfaceRef" :calcPricingParam="props.calcPricingParam.volSurface" :index="props.index"
                      type="volsurface"   :min="0"
                      :label-width="labelWidth" labelName="波动率" url="/pricing/comboBox/queryVolsurface.action"/>
  </el-form>

</template>

<script setup>
import curvePriceCombox from '@/xquant/base/curvePriceCombox'
import baseCombox from '@/xquant/base/baseCombox'
import dictCombox from '@/xquant/base/dictCombox'
import {getCurrentInstance, onBeforeUnmount, onMounted, reactive, ref, toRefs, watch} from 'vue'
import InputAmount from '../../../../xquant/base/InputAmount'
import {getDailysPrice} from "../instrument/common/common"
/*//期货标的  只有 贴现 波动率
//现货     四个  贴现 融资  波动 分红*/

/*返回参数*/
const {proxy} = getCurrentInstance()
const url = reactive({
  data: '/pricing/otcOption/queryEngineCfg.action'
})

const props = defineProps({
  calcPricingParam: {
    type: Object,
    default: {
      calcPPCode: '',
      dividend: {
        rate: '',
        dayCount: 'Actual/365'
      },
      volSurface: {
        rate: '',
        dayCount: 'Actual/365',
        annualDays: '252'
      },
      discount: {
        rate: '',
        dayCount: 'Actual/365'
      },
      spotPrice: '100',
      spotQuoteType: '0',
      customPricingParam: {}
    }
  },
  form:{
    type: Object,
    default:{}
  },formData:{
    type: Object,
    default:{}
  },
  index: {
    type: String,
    default: ''
  },
  valueDate: {
    type: String,
    default: ''
  },
  productType: '',
  exerciseType: '',
  filterList: {
    type: String,
    default: ''
  },
})
const data = reactive({
  filterList : []
})
const prules = {
  spotPrice: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
      return true;
    }, message: ' ', trigger: 'change'
  }]
}
const queryParam = computed(() => {
  return {
    productType: props.productType,
    exerciseType: props.exerciseType
  }
})
const calcPricingParam = reactive({
  spotQuoteType: '0',
  calcPPCode: '',
  spotPrice: '0',
  discount: {},
  dividend: {},
  volSurface: {}
})
watch(() => props.calcPricingParam, (nv, ov) => {
  setCalcPricingParam()
})

watch(() => calcPricingParam.spotPrice, (nv, ov) => {
  if(!["Vanilla","Digital","DoubleDigital"].includes(props.productType)) {
    calcRateByCode()
  }
})

const getCalcPricingParam = (calcType) => {
  if (isNaN(calcPricingParam.spotPrice)||Number(calcPricingParam.spotPrice)<=0){
    proxy.$modal.msgWarning("定价参数,标的价格不能为空！");
    // throw new Error(`定价参数,标的价格不能为空`)
    return {}
  }else {
    //add annualDays的处理 daycouter是Business/242(CHINA_EX) annualDays是242

    return {
      spotQuoteType: calcPricingParam.spotQuoteType,
      spotPrice: calcPricingParam.spotPrice,
      calcPPCode: calcPricingParam.calcPPCode,
      discount: proxy.$refs.discountRef.getCalcPricingParam(),
      dividend: proxy.$refs.dividendRef.getCalcPricingParam(),
      volSurface: proxy.$refs.volSurfaceRef.getCalcPricingParam(calcType) ,
    }
  }
}

const setSpotPriceProxy = () => {
  let query = {valueDate:props.valueDate,instrumentId:props.formData.instrumentId}
  setSpotPrice(query)
}

const setSpotPrice = (query) => {
  getDailysPrice(query).then((price) => {
    if (price) {
      switch (calcPricingParam.spotQuoteType) {
        case "0":calcPricingParam.spotPrice = price.dpRelTime;break;
        case "1":calcPricingParam.spotPrice = price.dpClose;break;
        case "2":calcPricingParam.spotPrice = price.dpSet;break;
        case "3":calcPricingParam.spotPrice = price.dpAvg;break;
      }
      data.filterList = price.filterList
    }
  })
}
const setCalcPricingParam = () => {
  calcPricingParam.calcPPCode = props.calcPricingParam.calcPPCode
  calcPricingParam.spotQuoteType = props.calcPricingParam.spotQuoteType || '0'
  if (props.calcPricingParam.spotPrice) {
    calcPricingParam.spotPrice = props.calcPricingParam.spotPrice
  }
  calcPricingParam.discount = props.calcPricingParam.discount
  calcPricingParam.dividend = props.calcPricingParam.dividend
  calcPricingParam.volSurface = props.calcPricingParam.volSurface
  let discount = props.calcPricingParam.discount
  let dividend = props.calcPricingParam.dividend
  let volSurface = props.calcPricingParam.volSurface
  proxy.$refs.discountRef.setCalcPricingParam(discount)
  proxy.$refs.dividendRef.setCalcPricingParam(dividend)
  proxy.$refs.volSurfaceRef.setCalcPricingParam(volSurface)
}

const calcRateByCode = () => {
   proxy.$refs.discountRef.getCalcPricingRateByCode('ircurve')
   proxy.$refs.dividendRef.getCalcPricingRateByCode('divyield')
   proxy.$refs.volSurfaceRef.getCalcPricingRateByCode('volsurface')
}
//指定 ref  暴露断点
defineExpose({
  getCalcPricingParam, setCalcPricingParam, setSpotPrice ,calcRateByCode
})
onMounted(() => {
  setCalcPricingParam()
})

</script>

<style scoped>
.wrapper {
  display: inline-grid;
  grid-template-columns: 8fr 1fr ;
  grid-column-gap: 10px;
}
</style>
